package com.example.demo.service.impl;

import com.example.demo.dao.UserDaoMapper;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * user服务实现类
 *
 * @author Eric Wu
 * @since 1.8
 * create 2020-06-08
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDaoMapper userDao;

    @Override
    public PageInfo<User> queryAllUser(Integer page, Integer pageSize) {
        PageInfo<User> result = PageHelper
                .startPage(page, pageSize)
                .doSelectPageInfo(() -> userDao.queryAllUsers());
        return result;
    }

    @Override
    public Optional<User> queryUserByName(String name) {
        if(StringUtils.hasText(name)){
            return Optional.of(userDao.queryUserByName(name));
        }
        return Optional.empty();
    }
}
